define(function(require){
	var $=require("jq");

		$('#activation-btn').bind('click',function(){
			var activateNum = $('#activation-code').val();
			if(activateNum == ""){
				alert('激活码不能为空，请输入激活码！');
				return;
			}
			var params = {};
			params['activateNum'] = activateNum;
			$.ajax({
				url:'/vipCenter/weiXinCouponActivate.do',
				dataType:'json',
				type:'POST',
				async:false,
				data:params,
				success:function(responseText){
					if(responseText['isLogin'] == 0){
						var loginView = responseText['loginFormView'];
						window.location.href=loginView;
						return;
					}else{
						if(responseText['ret'] == 1){
							alert('优惠券激活成功，马上开启您的购物之旅吧!');
							return;
						}else if(responseText['ret'] == -1){
							alert('您输入的优惠券有误，请重新输入！');
							return;
						}else if(responseText['ret'] == -2){
							alert('您已经领取过优惠券！');
							return;
						}else{
							alert('优惠券激活失败！');
							return;
						}
					}
				}
			});
		});
		
		
	//名望券操作
	var zenFame = $("#J_ZenFame");
	if(zenFame[0]){
		
		var Dialog        = require("/pinzhiV2.0/js/pz_use_dialog.js");
		var userNameInput = $("#js_user_name"),
			zenBtn        = $("#js_zen_btn"),
			fameList      = $("#js_tab_content input"),
			tabBar        = $("#js_tab_bar"),
			tabMain       = $("#js_tab_content");
		
		tabBar.click(function(){
			var txt = $(this).attr("data-caption");
			if(!$(this).data("is")){
				$(this).data("is",true).text("+"+txt);
			}else{
				$(this).data("is",false).text("-"+txt);
			};
			
			tabMain.toggle();
			
		});
		
		zenBtn.click(function(){
			
			var self        = $(this),
				userNameTxt = userNameInput.val().replace(/^\s+|\s+$/g,"");
			
			if(userNameTxt == ""){
				var tips=new Dialog({
									  type:"alert",
									  text:"提示",
									  tipsText:"请输入要赠送人的ID",
									  buttonSureText:"知道了", 
									  callBack:function(){
											userNameInput.focus();
									  },
									  mask:"transparent"            
									  });
				return false;
			};
			
			if(!fameList[0]){
				var tips=new Dialog({
									  type:"alert",
									  text:"提示",
									  tipsText:"你没有名望券！",
									  buttonSureText:"知道了", 
									  mask:"transparent"            
									  });
				return false;
			};
			//查看是否选择一个名望券
			if(getFameCoupons(fameList).length === 0){
				var tips=new Dialog({
									  type:"alert",
									  text:"提示",
									  tipsText:"请选择至少一张名望券赠送！",
									  buttonSureText:"知道了", 
									  mask:"transparent"            
									  });
				return false;
			};
			
			//防止重复提交
			if(!$(this).data("status")){
				var conform=new Dialog({
									  type:"confirm",
									  text:"提示",
									  tipsText:"确定要赠送名望券嘛？",
									  buttonSureText:"必须赠送", 
									  buttonCancelText:"考虑一下", 
									  callBack:function(){
											self.data("status",true).text("赠送中...");
											$.post("/vipCenter/giveFameTicketsOk.do",{userName:userNameTxt,fameList:getFameCoupons(fameList).join(",")},function(ret){
																self.data("status",false).text("继续赠送");
																if(ret.state == "ok"){
																	var tips=new Dialog({
																						  type:"alert",
																						  text:"提示",
																						  tipsText:"赠送成功！",
																						  buttonSureText:"知道了", 
																						  callBack:function(){
																								window.location.reload();
																						  },
																						  mask:"transparent"            
																						  });
																	
																}else{
																	var tips=new Dialog({
																						  type:"alert",
																						  text:"提示",
																						  tipsText:ret['msg'],
																						  buttonSureText:"再试一次", 
																						  callBack:function(){
																								//
																						  },
																						  mask:"transparent"            
																						  });		
																};
																
															},"json");
	
									  },
									  mask:0.3,
									  isDrag:true            
									  });
			};
		});
		
	};
	
	//获取名望券
	var getFameCoupons = function(fameList){
			var arr = [];
			fameList.each(function(i,o){
				if(this.checked){
					arr.push(this.value);
				};
			});
			return arr;
		};
			
});